using System;
using System.ComponentModel;
using System.Xml;
using System.Xml.Serialization;
using System.Text;

namespace Core.Mapping
{
    [Serializable()]
    [DefaultPropertyAttribute("LandLayer")]
    [XmlRoot("LandLayer")]
    public class LandLayer : IXmlSerializable
    {
        [XmlElement("LandTileCollection")]
        public LandTileCollection LandTiles { get { return _LandTiles; } set { _LandTiles = value; } }
        [XmlAttribute("Width")]
        public int Width { get { return _Width; } set { _Width = value; } }
        [XmlAttribute("Height")]
        public int Height { get { return _Height; } set { _Height = value; } }

        protected LandTileCollection _LandTiles;
        protected int _Width;
        protected int _Height;

        public LandLayer(LandTileCollection LandTiles, int Width, int Height)
        {
            _Width = Width;
            _Height = Height;
            _LandTiles = LandTiles;
        }

        public LandLayer(int Width, int Height)
        {
            _LandTiles = LandTileCollection.GetTilesForEmptyLandLayer(Width, Height);
            _Width = Width;
            _Height = Height;
        }

        public LandLayer()
        {
            //For XML Serialization
        }

        public LandTile GetTileAtLocation(int x, int y)
        {
            if (x >= 0 && y >= 0)
                return _LandTiles[y * Map.MapHeight + x];
            else
                return LandTile.DefaultLandTile;
        }

        public void SetTileAtLocation(LandTile tile, int x, int y)
        {
            if (x >= 0 && y >= 0)
                _LandTiles[y * Map.MapHeight + x] = tile;
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            //Read in basic properties
            _Width = Convert.ToInt32(reader.GetAttribute("Width"));
            _Height = Convert.ToInt32(reader.GetAttribute("Height"));
            //Create TileCollection
            _LandTiles = new LandTileCollection();

            //Read Land Tile String
            string strAllTiles = reader.GetAttribute("Tiles");
            
            //---load tiles---//
            //If the entire tile string is empty then fill with default value tiles
            if (strAllTiles == "")
            {
                for (int i = 0; i < _Width * _Height; i++)
                {
                    _LandTiles.Add(LandTile.DefaultLandTile);
                }
            }
            else
            {
                //if tiles exist, then load in each one, splitting on the comma
                char[] ch = new char[1];
                ch[0] = ',';

                string[] Tiles = strAllTiles.Split(ch);

                foreach (string str in Tiles)
                {
                    //if empty string then add blank tile
                    if (str == "")
                    {
                        _LandTiles.Add(LandTile.DefaultLandTile);
                    }
                    else
                    {
                        //if integer value exists, then load value from int
                        _LandTiles.Add(LandTile.GetLandTileFromBitVector(Convert.ToInt32(str)));
                    }
                }
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            //Write Properties to xml
            writer.WriteAttributeString("Width", _Width.ToString());
            writer.WriteAttributeString("Height", _Height.ToString());

            //----Write Tile Info---\\
            int notblank = 0;

            StringBuilder sb = new StringBuilder();

            foreach (LandTile t in _LandTiles)
            {
                if (t == LandTile.DefaultLandTile)
                {
                    //If this tile is default value add a ',' to let us know
                    sb.Append(',');
                }
                else
                {
                    //If this is a real tile then get it's int value and save
                    sb.Append(LandTile.GetLandTileAsBitVector(t).ToString() + ",");
                    //If this tile is not blank add one.
                    notblank++;
                }
            }

            if (notblank == 0)
            {
                //If there were no tiles in whole layer save empty string
                sb = new StringBuilder(string.Empty);
            }

            writer.WriteAttributeString("Tiles", sb.ToString());
        }

        #endregion

    }
}
